import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DOM_schreiben {

	
	public void schreiben() {
// DOM-Dokument ERSTELLEN 
		DocumentBuilderFactory fabrik= DocumentBuilderFactory.newInstance();
		DocumentBuilder dokumentBauer = null;
		try {
			dokumentBauer = fabrik.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Document dokument = dokumentBauer.newDocument();
		
// Terminkalender
		Element terminkalender = dokument.createElement("terminkalender");
		terminkalender.setAttribute("von","Humbert Humbert");
		dokument.appendChild(terminkalender); // ROOT

// Termine 
		Element termine = dokument.createElement("termine");
		termine.setAttribute("art","Wichtige Termine");

		Element kongresstermine = dokument.createElement("termine");
		kongresstermine.setAttribute("art", "Kongresse");
		
// Termin 1
		Element termin= dokument.createElement("termin");
		termin.setAttribute("nr","1");
			Element termin_was =  dokument.createElement("was");
			termin_was.setTextContent("Essen gehen");
		termin.appendChild(termin_was);
		
// Termin 2
		Element termin2= dokument.createElement("termin");
		termin2.setAttribute("nr","2");
			Element termin_was2 =  dokument.createElement("was");
			termin_was2.setTextContent("Schlafen");
		termin2.appendChild(termin_was2);

		// Kongress Termin 1
		Element termin3 = dokument.createElement("termin");
		termin3.setAttribute("nr","1");
		Element termin_was3 =  dokument.createElement("was");
		termin_was3.setTextContent("Schlafen");
		Element termin_wo3 = dokument.createElement("wo");
		termin_wo3.setTextContent("Donaustadtstra�e 2");
		termin3.appendChild(termin_was3);
		termin3.appendChild(termin_wo3);
				
		// Kongress Termin 2
		Element termin4 = dokument.createElement("termin");
		termin4.setAttribute("nr","2");
		Element termin_was4 =  dokument.createElement("was");
		termin_was4.setTextContent("Schlafen");
		Element termin_wo4 = dokument.createElement("wo");
		termin_wo4.setTextContent("Donaustadtstra�e 1");
		termin4.appendChild(termin_was4);
		termin4.appendChild(termin_wo4);
			
// Termine --> Termin 1 & Termin 2 einf�gen
		termine.appendChild(termin);
		termine.appendChild(termin2);
		kongresstermine.appendChild(termin3);
		kongresstermine.appendChild(termin4);
		
// Terminkalender --> "wichtige" Termine einf�gen 
		terminkalender.appendChild(termine);
		terminkalender.appendChild(kongresstermine);
		
// Dokument (formatiert) in Datei schreiben 
		TransformerFactory transformerFabrik = TransformerFactory.newInstance();
		Transformer transformer = null;
		
		try {
			transformer = transformerFabrik.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT,"yes"); 						// XML formatieren
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2"); // XML formatieren
			DOMSource quelle = new DOMSource(dokument);
			StreamResult ziel = new StreamResult(new File("test.xml")); // ausgabe ist ein Stream
			transformer.transform(quelle,ziel);
		} catch (TransformerConfigurationException e1) {

			e1.printStackTrace();
		}
		 catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		
	}
	
	
}


